package design;

import java.util.HashMap;

/**
 * 一个享元工厂，用来创建并管理Flyweight对象，主要用来确保合理地共享
 * Flyweight，当用户请求一个Flyweight时，FlyweightFactory对象提供一个已经创建的实例
 */
public class FlyweightFactory
{
    //定义一个池容器
    private static HashMap<String, Flyweight> pool = new HashMap<>();

    //享元工厂
    public static Flyweight getFlyweight(String extrinsic)
    {
        Flyweight flyweight = null;
        if (pool.containsKey(extrinsic))//池中有该对象
        {
            flyweight = pool.get(extrinsic);
            System.out.println("包含此对象" + extrinsic + "直接从池中取值----》");
        }else
        {
            //根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight(extrinsic);

            //放入池中
            pool.put(extrinsic, flyweight);
            System.out.println("创建" + extrinsic + "并从池中取出----》");
        }
        return flyweight;
    }
}
